using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class PortfolioTodayDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public PortfolioTodayDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the PortfolioToday table.
		/// </summary>
		public virtual void Insert(PortfolioTodayInfo portfolioTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", portfolioTodayInfo.IdPortfolio),
				new SqlParameter("@PostDate", portfolioTodayInfo.PostDate),
				new SqlParameter("@IdCommon", portfolioTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", portfolioTodayInfo.AccountNo),
				new SqlParameter("@SecSymbol", portfolioTodayInfo.SecSymbol),
				new SqlParameter("@SecType", portfolioTodayInfo.SecType),
				new SqlParameter("@Unit", portfolioTodayInfo.Unit),
				new SqlParameter("@PriceMkt", portfolioTodayInfo.PriceMkt),
				new SqlParameter("@IMStock", portfolioTodayInfo.IMStock),
				new SqlParameter("@IMCustomer", portfolioTodayInfo.IMCustomer),
				new SqlParameter("@AdjustRate", portfolioTodayInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PortfolioToday_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the PortfolioToday table.
		/// </summary>
		public virtual void Update(PortfolioTodayInfo portfolioTodayInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", portfolioTodayInfo.IdPortfolio),
				new SqlParameter("@PostDate", portfolioTodayInfo.PostDate),
				new SqlParameter("@IdCommon", portfolioTodayInfo.IdCommon),
				new SqlParameter("@AccountNo", portfolioTodayInfo.AccountNo),
				new SqlParameter("@SecSymbol", portfolioTodayInfo.SecSymbol),
				new SqlParameter("@SecType", portfolioTodayInfo.SecType),
				new SqlParameter("@Unit", portfolioTodayInfo.Unit),
				new SqlParameter("@PriceMkt", portfolioTodayInfo.PriceMkt),
				new SqlParameter("@IMStock", portfolioTodayInfo.IMStock),
				new SqlParameter("@IMCustomer", portfolioTodayInfo.IMCustomer),
				new SqlParameter("@AdjustRate", portfolioTodayInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PortfolioToday_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the PortfolioToday table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idPortfolio)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", idPortfolio)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PortfolioToday_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the PortfolioToday table.
		/// </summary>
		public virtual PortfolioTodayInfo Select(Guid? idPortfolio)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdPortfolio", idPortfolio)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PortfolioToday_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakePortfolioTodayInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the PortfolioToday table.
		/// </summary>
		public List<PortfolioTodayInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PortfolioToday_SelectAll"))
			{
				List<PortfolioTodayInfo> portfolioTodayInfoList = new List<PortfolioTodayInfo>();
				while (dataReader.Read())
				{
					portfolioTodayInfoList.Add(MakePortfolioTodayInfo(dataReader));
				}

				return portfolioTodayInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the PortfolioToday class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual PortfolioTodayInfo MakePortfolioTodayInfo(SqlDataReader dataReader)
		{
			PortfolioTodayInfo portfolioTodayInfo = new PortfolioTodayInfo();
			portfolioTodayInfo.IdPortfolio = SqlClientUtility.GetGuid(dataReader, DbConstants.PORTFOLIOTODAY.IDPORTFOLIO);
			portfolioTodayInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.PORTFOLIOTODAY.POSTDATE);
			portfolioTodayInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOTODAY.IDCOMMON);
			portfolioTodayInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOTODAY.ACCOUNTNO);
			portfolioTodayInfo.SecSymbol = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOTODAY.SECSYMBOL);
			portfolioTodayInfo.SecType = SqlClientUtility.GetString(dataReader, DbConstants.PORTFOLIOTODAY.SECTYPE);
			portfolioTodayInfo.Unit = SqlClientUtility.GetInt32(dataReader, DbConstants.PORTFOLIOTODAY.UNIT);
			portfolioTodayInfo.PriceMkt = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOTODAY.PRICEMKT);
			portfolioTodayInfo.IMStock = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOTODAY.IMSTOCK);
			portfolioTodayInfo.IMCustomer = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOTODAY.IMCUSTOMER);
			portfolioTodayInfo.AdjustRate = SqlClientUtility.GetDecimal(dataReader, DbConstants.PORTFOLIOTODAY.ADJUSTRATE);

			return portfolioTodayInfo;
		}

		#endregion
	}
}
